<template>
  <div class="departments">
    <div class="app-container">
      <el-card>
        <template #header>
          <!-- 用一个行列布局 -->
          <el-row>
            <el-col :span="20">
              <span>{{ company.name }}</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>负责人</el-col>
                <!-- 下拉菜单 element -->
                <el-col>
                  <el-dropdown>
                    <span> 操作<i class="el-icon-arrow-down" /> </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="openAddDept()">
                        添加顶级部门
                      </el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
        <el-tree :data="treedata" :props="defaultProps" default-expand-all>
          <template #default="{ data }">
            <el-row style="width: 100%">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager || "--" }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="openAddDept(data, 1)">
                          添加子部门
                        </el-dropdown-item>
                        <el-dropdown-item @click.native="openAddDept(data, 2)">
                          编辑部门
                        </el-dropdown-item>
                        <el-dropdown-item @click.native="delDept(data)">
                          删除部门
                        </el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- 对话框 -->
    <AddDept
      ref="ad"
      :show-dialog="showDialog"
      :dept-data="deptData"
      :parent-dept="parentDept"
      @refresh-dept="getTreeData"
      @close-dialog="showDialog = $event"
    />
  </div>
</template>

<script>
import { getDepartments, delDepartments } from '@/api/departments.js'
// 引入处理树形数据
import { tranformTreeData } from '@/utils/index.js'
import AddDept from './components/add-dept.vue'

export default {
  components: {
    AddDept
  },
  data () {
    return {
      // 树形结构数据
      treedata: [],
      // 没被处理的公司原数据
      deptData: [],
      company: { name: '' },
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 对话框
      showDialog: false,
      parentDept: null
    }
  },
  created () {
    this.getTreeData()
  },
  methods: {
    async getTreeData () {
      const { depts, companyName } = await getDepartments()
      this.deptData = depts
      this.treedata = tranformTreeData(depts)
      this.company.name = companyName
    },
    async delDept (curDept) {
      try {
        await this.$confirm(`确定删除：${curDept.name}吗`, '提示')
        if (curDept.children && curDept.children.length > 0) {
          return this.$message.error('不能删除父部门')
        }
        await delDepartments(curDept.id)
        this.getTreeData()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 打开新增部门弹层方法
    // parentDept 添加子部门的父部门数据 =》说明：顶级部门没有父，所以parentDept是undefined
    openAddDept (parentDept, type) {
      // 1. 存储子部门的父部门
      this.parentDept = parentDept
      // 2. 打开弹层
      this.showDialog = true
      if (type === 2) {
        this.$refs.ad.getDeptDailog(parentDept.id)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-tree-node__content {
  margin: 3px 0;
  font-size: 14px;
}
</style>
